package com.example.chapter04;


import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;


import java.util.Date;

public class ActionUriActivity extends AppCompatActivity implements View.OnClickListener {
    TextView tvSend;
    ActivityResultLauncher<Intent> register;
    TextView tv_response;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_action_uri);
        findViewById(R.id.btn_dial).setOnClickListener(this);
        findViewById(R.id.btn_sms).setOnClickListener(this);
        findViewById(R.id.btn_my).setOnClickListener(this);

        tv_response = findViewById(R.id.tv_response);

        tvSend = findViewById(R.id.tv_send);
        findViewById(R.id.btn_send).setOnClickListener(this);


        Log.d("+++++++++++", "进入register前");
        register = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                Log.d("+++++++++++", "进入了register");
                if (result != null) {
                    Intent data = result.getData();
                    if (data != null && result.getResultCode() == Activity.RESULT_OK) {
                        Bundle extras = data.getExtras();

                        if (extras != null) {
                            String time = extras.getString("time");

                            String response = extras.getString("response");
                            System.out.println(response);
                            if (time != null && response != null)
                                tv_response.setText(String.format("%s\n%s", time, response));
                        }
                    }
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        String phoneNo = "12345";
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.btn_dial:
                // 设置意图动作为准备拨号
                intent.setAction(Intent.ACTION_DIAL);
                // 声明一个拨号的Uri
                Uri uri = Uri.parse("tel:" + phoneNo);
                intent.setData(uri);
                startActivity(intent);
                break;
            case R.id.btn_sms:
                intent.setAction(Intent.ACTION_SENDTO);
                Uri uri1 = Uri.parse("sms:" + phoneNo);
                intent.setData(uri1);
                startActivity(intent);
                break;
            case R.id.btn_my:
                intent.setAction("android.intent.action.RAIN");
                intent.addCategory("android.intent.category.DEFAULT");
                startActivity(intent);
                break;
            case R.id.btn_send:
                Intent receive = new Intent(this, ActionReceiveActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("send", String.valueOf(tvSend.getText()));
                bundle.putString("time", new Date().toString());
                receive.putExtras(bundle);
                register.launch(receive);
                break;

            default:
                break;
        }
    }
}